# coding=utf-8
from time import sleep

from django.http import HttpResponse, FileResponse
from django.shortcuts import render

BASE_DIR = './'


def upload_file(request):
    if request.method == 'POST':
        file_obj = request.FILES.get('file')
        import os
        f = open(os.path.join(BASE_DIR, 'static', 'pic', file_obj.name), 'wb')
        print(file_obj, type(file_obj))
        for chunk in file_obj.chunks():
            f.write(chunk)
        f.close()
        return HttpResponse('OK')


def file_down(request, filename):
    print(request.get_full_path(), filename)
    file_path = 'static/pic/%s' % filename
    file = open(file_path, 'rb')
    response = FileResponse(file)
    response['Content-Type'] = 'application/octet-stream'
    response['Content-Disposition'] = 'attachment;filename=%s' % filename
    return response
